package com.huluobo.jetpackdemo.paging

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.huluobo.jetpackdemo.flow.UserBean
import kotlinx.coroutines.flow.Flow

/**
 *  Created by LC on 2024/11/14.
 */
class PagingViewModel : ViewModel() {
    fun getPagingData(): Flow<PagingData<Item>> {
        return Repo.getPagingData()
    }
}

object Repo {
    private const val PAGE_SIZE = 5
    private val apiService = ApiService.create()

    //pagingSourceFactory 生成出数据源的位置,代表一个方法
    //config paging的配置文件,pageSize 一次的加载数量 prefetchDistance 预加载的剩余的数量
    //flow 让产生的数据源形成一个flow可以发送给页面

    fun getPagingData(): Flow<PagingData<Item>> {
        return Pager(
            pagingSourceFactory = { RepoPagingSource(apiService) },
            config = PagingConfig(pageSize = PAGE_SIZE, prefetchDistance = PAGE_SIZE * 5)
        ).flow
    }
}